package com.conversationboard.batch.scheduler;

import java.util.Date;

import org.joda.time.MutableDateTime;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;

import com.conversationboard.batch.jobs.activeusers.CurrentlyActiveUsersJob;
import com.conversationboard.batch.jobs.bans.BanChecker;
import com.conversationboard.batch.jobs.bans.RemoveTemporaryBans;
import com.conversationboard.batch.jobs.cacherecord.CacheRecordJob;
import com.conversationboard.batch.jobs.fulltext.IndexDocuments;
import com.conversationboard.batch.jobs.loghousekeeping.UserLogHousekeepingJob;
import com.conversationboard.batch.jobs.polls.PollBumper;
import com.conversationboard.batch.jobs.recordthreadreads.RecordThreadReads;
import com.conversationboard.batch.jobs.runstats.RunstatsJob;
import com.conversationboard.batch.jobs.stats.CountersJob;
import com.conversationboard.batch.jobs.stats.Stats;
import com.conversationboard.batch.jobs.subscriptions.SubscriptionsJob;
import com.conversationboard.batch.jobs.user.UserHousekeeping;
import com.conversationboard.logger.Logger;

public class Jobs {

	private static Scheduler scheduler;


	public static Scheduler getScheduler() {
		return scheduler;
	}


	private static void setScheduler(Scheduler thisScheduler) {
		scheduler = thisScheduler;
	}


	/** This class is where all the batch jobs are initialised. */

	public static void initialise(Scheduler scheduler) {

		try {

			setScheduler(scheduler);

			/*--- Clear the user logs greater than 30 days old every day at 3am */

			JobDetail clearLogsJob = new JobDetail("Clear User Logs", null, UserLogHousekeepingJob.class);

			Trigger clearLogsTrigger = TriggerUtils.makeDailyTrigger(03, 00);
			clearLogsTrigger.setStartTime(new Date());
			clearLogsTrigger.setName("Clear User Logs Trigger");

			scheduler.scheduleJob(clearLogsJob, clearLogsTrigger);

			JobDetail pollBumperJob = new JobDetail("Poll Bumper", null, PollBumper.class);

			Trigger pollBumperTrigger = TriggerUtils.makeDailyTrigger(00, 01);
			pollBumperTrigger.setStartTime(new Date());
			pollBumperTrigger.setName("Poll Bumper Trigger");

			scheduler.scheduleJob(pollBumperJob, pollBumperTrigger);

			/* Check for users whose banning threshold has been reached */

			JobDetail banCheckerJob = new JobDetail("Ban Checker", null, BanChecker.class);

			/* Check for automated bans every day at 9am */
			
			Trigger banCheckerTrigger = TriggerUtils.makeDailyTrigger(9, 0);
			MutableDateTime banCheckerStartTime = new MutableDateTime();
			banCheckerStartTime.addMinutes(15);
			banCheckerTrigger.setStartTime(banCheckerStartTime.toDate());
			banCheckerTrigger.setName("Ban Checker Trigger");

			scheduler.scheduleJob(banCheckerJob, banCheckerTrigger);

			/* Index and reindex documents every ten minutes */

			JobDetail indexJob = new JobDetail("Indexer", null, IndexDocuments.class);

			Trigger indexerTrigger = TriggerUtils.makeMinutelyTrigger(30);
			MutableDateTime indexerStartTime = new MutableDateTime();
			indexerStartTime.addMinutes(10);
			indexerTrigger.setStartTime(indexerStartTime.toDate());
			indexerTrigger.setName("Indexer Trigger");

			scheduler.scheduleJob(indexJob, indexerTrigger);

			/* User Housekeeping */

			JobDetail userHousekeepingJob = new JobDetail("User Housekeeping", null, UserHousekeeping.class);

			Trigger userHousekeepingTrigger = TriggerUtils.makeDailyTrigger(22, 00);
			userHousekeepingTrigger.setStartTime(new Date());
			userHousekeepingTrigger.setName("User Housekeeping Trigger");

			scheduler.scheduleJob(userHousekeepingJob, userHousekeepingTrigger);

			/* Statistics */

			JobDetail statisticsJob = new JobDetail("Statistics", null, Stats.class);

			Trigger statisticsTrigger = TriggerUtils.makeDailyTrigger(02, 30);
			statisticsTrigger.setStartTime(new Date());
			statisticsTrigger.setName("Statistics Trigger");

			scheduler.scheduleJob(statisticsJob, statisticsTrigger);

			
			/* Email Subscriptions Job */

			JobDetail subscriptionsJob = new JobDetail("Subscriptions Job", null, SubscriptionsJob.class);

			Trigger subscriptionsTrigger = TriggerUtils.makeDailyTrigger(12, 00);
			subscriptionsTrigger.setStartTime(new Date());
			subscriptionsTrigger.setName("Subscriptions Trigger");

			scheduler.scheduleJob(subscriptionsJob, subscriptionsTrigger);

			/* Record Cache Sizes */

			JobDetail cacheRecordJob = new JobDetail("Cache Record Job", null, CacheRecordJob.class);

			Trigger cacheRecordTrigger = TriggerUtils.makeDailyTrigger(23, 00);
			cacheRecordTrigger.setStartTime(new Date());
			cacheRecordTrigger.setName("Cache Record Trigger");

			scheduler.scheduleJob(cacheRecordJob, cacheRecordTrigger);

			/* Runstats: 1st day of the month at 4am */

			JobDetail runstatsJob = new JobDetail("Runstats Job", null, RunstatsJob.class);

			Trigger runstatsTrigger = TriggerUtils.makeMonthlyTrigger(1, 4, 0);
			runstatsTrigger.setStartTime(new Date());
			runstatsTrigger.setName("Runstats Trigger");

			scheduler.scheduleJob(runstatsJob, runstatsTrigger);

			/* Revoke Temporary Bans */

			JobDetail revokeTemporaryBansJob = new JobDetail("Revoke Temporary Bans Job", null, RemoveTemporaryBans.class);

			Trigger revokeTemporaryBansTrigger = TriggerUtils.makeDailyTrigger(00, 20);
			revokeTemporaryBansTrigger.setStartTime(new Date());
			revokeTemporaryBansTrigger.setName("Revoke Temporary Bans Trigger");

			scheduler.scheduleJob(revokeTemporaryBansJob, revokeTemporaryBansTrigger);

			/* Initialise Counters */

			JobDetail initialiseCountersJob = new JobDetail("Initialise Counters", null, CountersJob.class);

			Trigger initialiseCountersTrigger = TriggerUtils.makeDailyTrigger(00, 25);
			initialiseCountersTrigger.setStartTime(new Date());
			initialiseCountersTrigger.setName("Initialise Counters Trigger");

			scheduler.scheduleJob(initialiseCountersJob, initialiseCountersTrigger);

			/* Read Thread Counts and copy into Site object*/

			JobDetail readThreadCountsJob = new JobDetail("ReadThreadCounts", null, RecordThreadReads.class);

			Trigger readThreadCountsTrigger = TriggerUtils.makeHourlyTrigger();
			readThreadCountsTrigger.setStartTime(new Date());
			readThreadCountsTrigger.setName("Update Thread Read Counts Trigger");

			scheduler.scheduleJob(readThreadCountsJob, readThreadCountsTrigger);

			/* Update the currently active users every minute */

			JobDetail activeUsersJob = new JobDetail("Currently Active Users", null, CurrentlyActiveUsersJob.class);

			Trigger activeUsersTrigger = TriggerUtils.makeMinutelyTrigger(1);
			activeUsersTrigger.setStartTime(new Date());
			activeUsersTrigger.setName("Currently Active Users Trigger");

			scheduler.scheduleJob(activeUsersJob, activeUsersTrigger);

			
		} catch (SchedulerException e) {
			Logger.log(new Date(), e.getMessage(), null);
		}

	}

}
